﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace RemoteObject
{
    public class DistributedFileRequestor
    {
        private RemoteObject.MyRemoteObject proxyObjectTcp;
        private string jobName;


        public DistributedFileRequestor(string jobName)
        {
            this.jobName = jobName;
            proxyObjectTcp = (RemoteObject.MyRemoteObject)Activator.GetObject(typeof(RemoteObject.MyRemoteObject), Common.CloudServerURL);
        }

        public void Run()
        {
            DownloadJob job  = proxyObjectTcp.RequestDistributedJob(jobName);
            if (job != null)
            {
                while(!(job.GetStatus() == Common.Status.Completed  && job.GetServerURLs().Contains(Common.CloudServerURL)))
                {
                   job = proxyObjectTcp.RequestDistributedJob(jobName);
                   Thread.Sleep(2000);
                }
                String cloudPath = String.Format(@"C:\c\{0}", job.GetJobName());
                DownloadJob myDownload = new DownloadJob(cloudPath, cloudPath, Common.CloudServerURL);
                Thread t1 = new Thread(new ThreadStart(myDownload.StartDownload));
                t1.Start();
            }
        }
    }
}
